//    Copyright (C) Kherty.  All rights reserved.
using System;
using OpenLS.Core.Serialization;

namespace OpenLS.Spreadsheet
{
    class DataBar: IOfficeXmlSerializable
    {
        #region IOfficeXmlSerializable Members

        void IOfficeXmlSerializable.ReadXml(ReadContext context)
        {
            using (var c = context.Read("dataBar"))
            {
                MinimumLength = c.GetOptionalInteger("minLength", 10);
                MaximumLength = c.GetOptionalInteger("maxLength", 90);
                ShowValue = c.GetOptionalBoolean("showValue", true);
                ValueObject1 = c.ReadElement<ConditionalFormatValueObject>();
                ValueObject2 = c.ReadElement<ConditionalFormatValueObject>();
                Color = SpreadsheetColor.ReadColor("color", c);
            }
        }

        void IOfficeXmlSerializable.WriteXml(WriteContext context)
        {
            using (var c = context.Write("dataBar"))
            {
                c.SetOptionalInteger("minLength", MinimumLength, 10);
                c.SetOptionalInteger("maxLength", MaximumLength, 90);
                c.SetOptionalBoolean("showValue", ShowValue, true);
                c.WriteElement(ValueObject1);
                c.WriteElement(ValueObject2);
                Color.WriteXmlAs("color", c);
            }
        }

        private ConditionalFormatValueObject _valueObject1 = new ConditionalFormatValueObject(ConditionalFormatValueObjectType.Minimum, "0");
        public ConditionalFormatValueObject ValueObject1
        {
            get { return _valueObject1; }
            internal set { _valueObject1 = value; }
        }

        private ConditionalFormatValueObject _valueObject2 = new ConditionalFormatValueObject(ConditionalFormatValueObjectType.Maximum, "0");
        public ConditionalFormatValueObject ValueObject2
        {
            get { return _valueObject2; }
            internal set { _valueObject2 = value; }
        }

        public SpreadsheetColor Color { get; set; }
        private int _minimumLength = 10;
        public int MinimumLength
        {
            get { return _minimumLength; }
            set {
                if (value < 0 || value > 100)
                throw new ArgumentException("value");
                _minimumLength = value; }
        }

        private int _maximumLength = 90;
        public int MaximumLength
        {
            get { return _maximumLength; }
            set
            {
                if (value < 0 || value > 100)
                    throw new ArgumentException("value");
                _maximumLength = value;
            }
        }

        private bool _showValue = true;
        public bool ShowValue
        {
            get { return _showValue; }
            set { _showValue = value; }
        }

        #endregion

        internal  DataBar Clone()
        {
            var result = (DataBar) MemberwiseClone();
            result.ValueObject1 = ValueObject1.Clone();
            result.ValueObject2 = ValueObject2.Clone();
            return result;
        }
    }
}
